package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
//import org.apache.commons.lang3.StringUtils; lang3 注意不要到错包 这个里面有isnotBlank
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {

        //判断参数是否为空
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //如果当前页和显示条数数据不合规 改为初始的页数和显示条数
        dto.checkParam();

        //在下面调用Mybatis-Plus的page分页查询方法时 需要俩参数
        //这个page对象 是其中一个参数 要封装分页查询时的当前页和显示条数
        Page page = new Page(dto.getPage(), dto.getSize());

        //这个是page方法中第二个参数   是一个总的 条件对象
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        //如果前端写了name的参数查询
        if (!StringUtils.isEmpty(dto.getName())) {
            //就拼装在条件对象中  使用表达式  用参数的name值去查数据库中对应实体类的name值
            lambdaQueryWrapper.like(AdChannel::getName, dto.getName());
        }

        //调用分页方法
        IPage pageList = page(page, lambdaQueryWrapper);

        //封装返回值
        PageResponseResult pageResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) pageList.getTotal());

        //查询到的数据封装进去
        pageResult.setData(pageList.getRecords());
        return pageResult;
    }


    @Override
    public ResponseResult insert(AdChannel adChannel) {

        if (adChannel == null || StringUtils.isEmpty(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        adChannel.setCreatedTime(new Date());

        save(adChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult update(AdChannel adChannel) {
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        updateById(adChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult deleteById(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdChannel adChannel = getById(id);

        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "启用状态，不能删除！");
        }

        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
